<template>
  <div class="container">
    <el-card>
      <el-form
        ref="formRef"
        :inline="true"
        :model="form"
        :rules="rules"
        class="demo-form-inline"
        label-position="top"
      >
        <el-form-item label="仓库编码">
          <el-input v-model="form.code" style="width: 390px" :disabled="true" />
        </el-form-item>

        <el-form-item label="仓库名称" prop="name">
          <el-input
            v-model="form.name"
            placeholder="请输入"
            style="width: 390px"
          />
        </el-form-item>

        <el-form-item label="仓库类型" prop="type">
          <el-select
            v-model="form.type"
            style="width: 390px"
            placeholder="请选择"
          >
            <el-option label="中转仓" value="ZZ" />
            <el-option label="加工仓" value="JG" />
            <el-option label="储备仓" value="CB" />
            <el-option label="冷藏仓" value="LC" />
          </el-select>
        </el-form-item>

        <el-form-item label="省/市/区" style="width: 710px" prop="location">
          <el-cascader
            v-model="locate"
            style="width: 780px"
            :options="options"
            placeholder="请选择"
            @change="handleChange"
          />
        </el-form-item>
        <el-form-item label="详细地址">
          <el-input
            v-model="form.address"
            style="width: 780px"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="仓库状态" prop="status">
          <el-radio-group v-model="form.status" style="width: 440px">
            <el-radio :label="0">停用</el-radio>
            <el-radio :label="1">启用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="仓库面积">
          <el-input
            v-model="form.surface"
            placeholder="请输入"
            style="width: 390px"
          >
            <template slot="append">m³</template>
          </el-input>
        </el-form-item>
        <el-form-item label="负责人" prop="personName">
          <el-input
            v-model="form.personName"
            placeholder="请输入"
            style="width: 390px"
          />
        </el-form-item>

        <el-form-item label="联系电话" prop="phone">
          <el-input
            v-model="form.phone"
            placeholder="请输入"
            style="width: 390px"
          />
        </el-form-item>
        <div class="btn">
          <el-button
            type="success"
            round
            style="width: 100px"
            @click="goback"
          >返回</el-button>
          <el-button
            type="success"
            round
            style="width: 100px"
            @click="submit"
          >提交</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { loadck, addStore, Feedback, rework } from '@/api'
import { regionData, CodeToText } from 'element-china-area-data'
export default {
  data() {
    return {
      locate: [], // 详细地址
      form: {
        // 表单内容
        code: '',
        name: '',
        type: '',
        location: '', // 城市选择器
        address: '',
        status: 1,
        surface: '',
        personName: '',
        phone: '',
        area: '',
        city: '',
        province: ''
      },
      options: regionData, // 级联选择器
      rules: {
        name: [{ required: true, message: '必填', trigger: 'blur' }],
        type: [{ required: true, message: '必填', trigger: 'blur' }],
        locate: [{ required: true, message: '必填', trigger: 'blur' }],
        status: [{ required: true, message: '必选' }],
        personName: [{ required: true, message: '必填', trigger: 'blur' }],
        phone: [
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '请输入正确的手机号',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  async created() {
    if (this.$route.params.id !== 'null') {
      // 编辑回显
      const res1 = await Feedback(this.$parent.id)
      // console.log(res1)
      this.form = res1.data
      this.locate = [this.form.province, this.form.city, this.form.area]
    } else {
      // 新增调用ck接口得到的值
      const res = await loadck()
      this.form.code = res.data
    }
  },
  methods: {
    handleChange(val) {
      this.form.location = CodeToText[val[0]] + '/' + CodeToText[val[1]] + '/' + CodeToText[val[2]]
      // this.arealist = loc.slice(1)
      this.form.province = val[0]
      this.form.city = val[1]
      this.form.area = val[2]
    },
    async submit() { // 新增提交
      this.$refs.formRef.validate(async(valid) => {
        if (!valid) return
        if (!this.form.id) { // 新增接口
          await addStore(this.form)
          this.$message.success('新增成功')
          this.goback()
        } else { // 编辑
          await rework(this.form)
          this.$message.success('编辑成功')
          this.goback()
        }
      })
    },
    goback() { // 返回
      this.$emit('back')
    }
  }
}
</script>

<style lang="scss">
.btn {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
